﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Quartz.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_MaterialBusiness : BaseBusiness<PB_Material>, IPB_MaterialBusiness, ITransientDependency
    {
        public async Task<PB_Material> GetTheDataAsync(string id)
        {
            //var q = this.GetIQueryable().Include(i => i.MaterialType).Include(i => i.Measure).Include(i => i.Supplier).Include(i => i.Customer).Include(i=>i.ProcecureI).Include(i=>i.UnitGroupId);
            var q = this.GetIQueryable();
            return await q.SingleOrDefaultAsync(w => w.Id == id);
        }
        public async Task<List<PB_Material>> GetQueryData(SelectQueryDTO search)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Material>();
            if (!search.Keyword.IsNullOrEmpty())
                where = where.And(w => w.Name.Contains(search.Keyword) || w.Code.Contains(search.Keyword) || w.BarCode.Contains(search.Keyword));

            var result = await q.Where(where).OrderBy(o => o.Name).Take(search.Take).ToListAsync();
            if (!search.Id.IsNullOrEmpty())
            {
                var one = await this.GetIQueryable().Where(w => w.Id == search.Id).SingleOrDefaultAsync();
                result.Add(one);
            }
            return result;
        }

        public async Task<PageResult<PB_Material>> QueryDataListAsync(PageInput<PBMaterialConditionDTO> input)
        {
            var q = GetIQueryable().Include(i => i.MaterialType).Include(i => i.UnitGroup).Include(i => i.ProcecureI);
            var where = LinqHelper.True<PB_Material>();
            var search = input.Search;

            if (!search.TypeId.IsNullOrWhiteSpace()) where = where.And(p => p.MaterialTypeId == search.TypeId);
            if (!search.ContactName.IsNullOrWhiteSpace()) where = where.And(p => p.Supplier.Name.Contains(search.ContactName) || p.Supplier.Code.Contains(search.ContactName) || p.Customer.Name.Contains(search.ContactName) || p.Customer.Code.Contains(search.ContactName));
            if (!search.Keyword.IsNullOrWhiteSpace()) where = where.And(p => p.Name.Contains(search.Keyword) || p.Code.Contains(search.Keyword) || p.BarCode.Contains(search.Keyword) || p.SimpleName.Contains(search.Keyword));


            return await q.Where(where).GetPageResultAsync(input);
        }


        public async Task<PB_Material> GetByBarcode(string code)
        {
            return await this.GetIQueryable().SingleOrDefaultAsync(w => w.BarCode == code);
        }

        public async Task<List<PB_MaterialDTO>> QueryDataQueryAsync(string MId)
        {
            Expression<Func<PB_Material, PB_MaterialType, PB_MaterialDTO>> select = (a, b) => new PB_MaterialDTO
            {
                MaterialID = a.Id,//物料编号
                MaterialName = a.Name,//物料名称
                MaterialTypeId = b.Name,//类型名称
                MaterialCode = a.Code,//物料编号
                Weight = a.Weight,//理论重量
                TypeID = b.Id//物料类型id
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_MaterialType>() on a.MaterialTypeId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            //筛选
            if (!MId.IsNullOrEmpty())
            {
                q = q.Where(w => w.Id.Contains(MId));
            }
            else {
                return null;
            }
            return await q.ToListAsync();
        }
    }
}